package com.world.business.treasure.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.world.business.treasure.domain.Treasure;
import com.world.business.treasure.domain.query.TreasureAddRequest;
import com.world.business.treasure.domain.query.TreasurePageRequest;
import com.world.business.treasure.domain.query.TreasureRequest;
import com.world.business.treasure.domain.query.TreasureUpdateRequest;
import com.world.business.treasure.domain.vo.TreasureVO;
import com.world.business.treasure.service.ITreasureService;
import com.world.common.controller.BaseController;
import com.world.common.domain.ResponseData;
import com.world.common.domain.PageResult;
import com.world.common.domain.SuccessResponseData;
import com.world.common.enumeration.DelFlagEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 灵兽/灵植 前端控制器
 * </p>
 *
 * @author 邓驴
 * @since 2023-07-31
 */
@Api(tags = "灵兽/灵植")
@RestController
@RequestMapping("/treasure")
public class TreasureController extends BaseController<Treasure> {

    @Resource
    private ITreasureService treasureService;

    /**
     * 列表
     *
     * @param request 请求参数
     * @return {@link ResponseData} 列表
     * @author WangYX
     * @date 2023/07/31 15:24
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    public ResponseData<List<TreasureVO>> list(TreasureRequest request) {
        List<TreasureVO> list = treasureService.list(request);
        return new SuccessResponseData<>(list);
    }

    /**
     * 分页-列表
     *
     * @param request 请求参数
     * @return {@link ResponseData}
     * @author WangYX
     * @date 2023/07/31 15:25
     */
    @ApiOperation("分页-列表")
    @GetMapping("/page")
    public ResponseData<PageResult<TreasureVO>> page(TreasurePageRequest request) {
        Page<Treasure> page = startPage(request);

        LambdaQueryWrapper<Treasure> queryWrapper = Wrappers.lambdaQuery(Treasure.class);
        queryWrapper.eq(Treasure::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.like(Treasure::getName, request.getName());
        Page<Treasure> result = treasureService.page(page, queryWrapper);

        PageResult<TreasureVO> pageResult = getPageResult(result, TreasureVO.class);
        return new SuccessResponseData<>(pageResult);
    }

    /**
     * 新增
     *
     * @param request 请求参数
     * @return {@link ResponseData}
     * @author WangYX
     * @date 2023/07/31 15:32
     */
    @ApiOperation("新增")
    @PostMapping("/add")
    public ResponseData<Long> add(@RequestBody TreasureAddRequest request) {
        return new SuccessResponseData<>(treasureService.add(request));
    }

    /**
     * 修改
     *
     * @param request 请求参数
     * @return {@link ResponseData}
     * @author WangYX
     * @date 2023/07/31 15:37
     */
    @ApiOperation("修改")
    @PutMapping("/update")
    public ResponseData<Void> update(@RequestBody TreasureUpdateRequest request) {
        treasureService.update(request);
        return new SuccessResponseData<>();
    }

    /**
     * 删除
     *
     * @param id id
     * @return {@link ResponseData}
     * @author WangYX
     * @date 2023/07/31 15:17
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}}")
    public ResponseData<Void> delete(@PathVariable("id") Long id) {
        treasureService.delete(id);
        return new SuccessResponseData<>();
    }

    /**
     * 详情
     *
     * @param id id
     * @return {@link ResponseData}
     * @author WangYX
     * @date 2023/07/31 15:16
     */
    @ApiOperation("详情")
    @GetMapping("/{id}")
    public ResponseData<TreasureVO> get(@PathVariable("id") Long id) {
        return new SuccessResponseData<>(treasureService.get(id));
    }


}
